package com.qzdsoft.erpcloud.controller.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.basic.BillDeocdeType;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.basic.BillDeocdeTypeService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.query.basic.BillDeocdeTypeInfo;
import com.qzdsoft.erpcloud.vo.query.basic.BillDeocdeTypeQueryParam;
import com.qzdsoft.utils.CommonSfEnum;
//import com.qzdsoft.erpcloud.util.SessionUtil;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.basic.BasicPreinstallEnum;

/**
 * Created by Admin
 */
@Controller
@RequestMapping("/basic/bill/deocde/type")
public class BillDeocdeTypeController extends BaseController{

//    @Autowired
//    private RedisHelper redisHelper;
    
    @Autowired
    CenterCompanyService actCenterCompanyService;

    @Autowired
    private BillDeocdeTypeService basicBillDeocdeTypeService;

    /**
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {
        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();

        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }

        List<EnumPo> sfList = new ArrayList<EnumPo>();
        Map<Object, Object> sfMap = new HashMap<Object, Object>();

        for(CommonSfEnum e : CommonSfEnum.values()){
            SysUtils.enums(sfList, sfMap, e.getKey(), e.getVal());
        }
        
        model.addAttribute("preinstallList", preinstallList);
        model.addAttribute("preinstallMap", preinstallMap);

        model.addAttribute("sfList", sfList);
        model.addAttribute("sfMap", sfMap);

        model.addAttribute("actCenterCompanyMap",actCenterCompanyService.getAllCompany());

        return "basic/bill_deocde/index";
    }

    /**
     *
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/edit")
    public ModelAndView edit(Integer companyId,Integer id) {
        ModelAndView mv = new ModelAndView();
        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();

        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }

        List<EnumPo> sfList = new ArrayList<EnumPo>();
        Map<Object, Object> sfMap = new HashMap<Object, Object>();

        for(CommonSfEnum e : CommonSfEnum.values()){
            SysUtils.enums(sfList, sfMap, e.getKey(), e.getVal());
        }
        mv.addObject("preinstallList", preinstallList);
        mv.addObject("preinstallMap", preinstallMap);

        mv.addObject("sfList", sfList);
        mv.addObject("sfMap", sfMap);

        mv.addObject("actCenterCompanyMap",actCenterCompanyService.getAllCompany());
        if(null != id){
            BillDeocdeType  billDeocdeType = basicBillDeocdeTypeService.selectById(id);
            mv.addObject("billDeocdeType", billDeocdeType);
            mv.setViewName("basic/bill_deocde/edit");
        }else {
            mv.setViewName("basic/bill_deocde/add");
        }
        return mv;
    }

    /**
     * 执行编辑
     *
     * @param request
     * @param basicBillDeocdeType
     * @return
     */
    @ResponseBody
    @SysLog(description="编辑单据编码设置保存")
    @RequestMapping("/edit_execute")
    public Response<String> editExecute(HttpServletRequest request, BillDeocdeType basicBillDeocdeType) {
//        basicBillDeocdeType.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicBillDeocdeType.getCompany_id()));
        return basicBillDeocdeTypeService.saveOrUpd(basicBillDeocdeType);
    }
    /**
     * 执行新增
     *
     * @param request
     * @param basicBillDeocdeType
     * @return
     */
    @ResponseBody
    @SysLog(description="新增单据编码设置保存")
    @RequestMapping("/create_execute")
    public Response<String> createExecute(HttpServletRequest request, BillDeocdeType basicBillDeocdeType) {
//        basicBillDeocdeType.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicBillDeocdeType.getCompany_id()));
        return basicBillDeocdeTypeService.saveOrUpd(basicBillDeocdeType);
    }

    /**
     * 根据主键删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="删除单据编码设置")
    @RequestMapping("/del_execute")
    public Response<String> delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return basicBillDeocdeTypeService.del(id);
    }
    
    
    @ResponseBody
    @SysLog(description="查询单据编码设置")
    @RequestMapping("/data")
    public LayTableResponse<BillDeocdeTypeInfo> data(HttpServletRequest request, BillDeocdeTypeQueryParam param) {
//        Integer companyId = SessionUtil.getLoggedCompanyId(request);
//        if(null != companyId){
//            param.setCompanyId(companyId);
//        }

        return basicBillDeocdeTypeService.selectData(param.getName(), param.getCompanyId(), param.getIsDefault(), param.getIsPreinstall(), param.getPage(), param.getLimit());
    }

}
